﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CaifenWeb.UtilityCls;
using CF.Data.Entity.FrmMemberEntity;
using CF.Member.DAL.Member;
using Ext.Net.MVC;

namespace CaifenWeb.Areas.BgMgr.Controllers
{
    public class WebAdminController : Controller
    {
        [BgAuthorize]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string userName, string password)
        {
            int uid;
            FM_MainMemberDAL mdal = new FM_MainMemberDAL();
            if (mdal.Verify(userName, password, out uid))
            {
                Session["bgadmin"] = userName;
                mdal.MarkLoginInfo(uid, Request.UserHostAddress);
                return RedirectToAction("Index");
            }
            else
            {
                ViewData["ErrMsg"] = "用户名或者密码错误！";
                return View();
            }
        }

        public ActionResult Logout()
        {
            Session.Remove("bgadmin");
            return RedirectToAction("Login");
        }

        [BgAuthorize]
        public ActionResult AllAdmins()
        {
            return View();
        }

        [BgAuthorize]
        public AjaxStoreResult GetAllAdmins()
        {
            FM_MainMemberDAL mdal = new FM_MainMemberDAL();
            List<FM_MainMember> memlist = mdal.GetAllMember();
            return new AjaxStoreResult(memlist, memlist.Count);
        }

        [BgAuthorize]
        public ActionResult ChangePsw()
        {
            return View();
        }

        [HttpPost]
        [BgAuthorize]
        public AjaxFormResult ChangePsw(string NewPsw, string CheckPsw)
        {
            AjaxFormResult response = new AjaxFormResult();
            if (!NewPsw.Equals(CheckPsw))
            {
                response.Success = false;
                response.ExtraParams["msg"] = "密码不一致";
            }
            else if (NewPsw.Contains(" "))
            {
                response.Success = false;
                response.ExtraParams["msg"] = "密码不能含有空格字符";
            }
            else
            {
                FM_MainMemberDAL mdal = new FM_MainMemberDAL();
                string userName = Session["bgadmin"].ToString();
                mdal.ChangePassword(userName, NewPsw);
            }
            return response;
        }
    }
}
